package cc.imos.config;

import cc.imos.config.BasicApp;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 *
 * Created by asher on 2017/4/8.
 */
@Configuration
@EnableSwagger2
public class BasicSwagger2 {
    // http://localhost:8080/swagger-ui.html
    // Swagger2默认将所有的Controller中的RequestMapping方法都会暴露，
    // 然而在实际开发中，我们并不一定需要把所有API都提现在文档中查看，这种情况下，使用注解
    // @ApiIgnore来解决，如果应用在Controller范围上，则当前Controller中的所有方法都会被忽略，
    // 如果应用在方法上，则对应用的方法忽略暴露API

    @Autowired
    private BasicApp basicApp;

    @Value(value = "${swagger.basePackage}")
    private String basePackage;

    @Value(value = "${swagger.termsOfServiceUrl}")
    private String termsOfServiceUrl;

    @Value(value = "${swagger.name}")
    private String name;

    @Value(value = "${swagger.url}")
    private String url;

    @Value(value = "${swagger.email}")
    private String email;

    @Bean
    public Docket createRestApi() {
        ParameterBuilder accessToken = new ParameterBuilder();
        accessToken.name("accessToken").description("账号令牌").modelRef(new ModelRef("String")).parameterType("header").required(true).build();

        List<Parameter> aParameters = Lists.newArrayList(accessToken.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .consumes(Sets.newHashSet(MediaType.APPLICATION_JSON_UTF8_VALUE))
                .produces(Sets.newHashSet(MediaType.APPLICATION_JSON_UTF8_VALUE))
                .apiInfo(apiInfo())
                .globalOperationParameters(aParameters)
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        String title = basicApp.getName() + " - 应用接口";
        String description = "暂无描述信息！";
        return new ApiInfoBuilder().title(title).description(description)
                .termsOfServiceUrl(termsOfServiceUrl)
                .contact(new Contact(name, url, email))
                .version("0.1.0-SNAPSHOT")
                .license("爱萌信息")
                .licenseUrl("//imos.cc")
                .build();
    }
}
